<template>
  <div>
    <el-card>
      <el-button type="success" size="large" round @click="dialogVisible = true">添加标签</el-button>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="阶段" :sortable="true" :sort-method="sort">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-tag size="large" style="font-size: 18px;" :type="types[scope.row.grade - 1]">
                {{ names[scope.row.grade - 1] }}
              </el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="技术">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <span style="margin-left: 10px">{{ scope.row.title }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" round @click="handleEdit(scope.row)" :disabled="scope.row.default">编辑</el-button>
            <el-popconfirm title="你确定要删除吗" confirm-button-text="确认" cancel-button-text="取消"
              @confirm="handleDelete(scope.row)">
              <template #reference>
                <el-button type="danger" round :disabled="scope.row.default">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="添加标签" style="border-radius: 20px; font-weight: bold;" :center="true">
      <el-form :model="addFrom">
        <el-form-item label="技术：" prop="title" label-width="80px">
          <el-input v-model="addFrom.title" autocomplete="on" />
        </el-form-item>
        <el-form-item label="阶段：" prop='grade' label-width="80px">
          <el-select v-model="addFrom.grade" placeholder="请选择阶段" style="width: 240px" size="large">
            <el-option v-for="(item, index) in names" :key="index" :label="item" :value="index + 1" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addConfirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog v-model="dialogUpdateVisible" title="更新标签" @close="updateConfirm"
      style="border-radius: 20px; font-weight: bold;" :center="true">
      <el-form :model="updateForm" @keyup.enter="updateConfirm">
        <el-form-item label="技术：" prop="title" label-width="80px">
          <el-input v-model="updateForm.title" autocomplete="on" label-width="80px" />
        </el-form-item>
        <el-form-item label="标签：" prop="grade" label-width="80px">
          <el-select v-model="updateForm.grade" clearable placeholder="请选择阶段" style="width: 240px">
            <el-option v-for="(item, index) in names" :key="index" :label="item" :value="index + 1" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogUpdateVisible = false">取消</el-button>
          <el-button type="primary" @click="updateConfirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
const names = ['第一阶段', '第二阶段', '第三阶段']
const types = ['success', 'warning', 'danger']
//表格数据
const tableData = ref([])
//添加的数据
const addFrom = reactive({
  title: '',
  grade: ''
})
//更新的数据
const updateForm = reactive({
  title: '',
  grade: ''
})
//更新数据一起携带给后端修改数据库
const updateList = ref({})
const dialogVisible = ref(false)
onMounted(() => {
  getTableData()
})
const dialogUpdateVisible = ref(false)
const handleDelete = async (item) => {
  await axios.delete(`/admin/tags/${item._id}`)
  await getTableData()
}
const getTableData = async () => {
  const res = await axios.get('/admin/tags')
  console.log('tableData', res.data);
  tableData.value = res.data
}
//发起添加请求
const addConfirm = async () => {
  dialogVisible.value = false
  console.log('addFrom', addFrom);
  await axios.post('/admin/tags', addFrom)
  await getTableData()
  addFrom.title = ""
  addFrom.grade = ""
}
//发起更新请求
const updateConfirm = async () => {
  dialogUpdateVisible.value = false
  await axios.put(`/admin/tags/${updateList.value._id}`,updateForm)
  await getTableData()
}
const handleEdit = (item) => {
  dialogUpdateVisible.value = true
  updateForm.title = item.title
  updateForm.grade = item.grade
  updateList.value = item
}
//排序
const sort = (rowa, rowb) => {
  return rowa.grade - rowb.grade
}
</script>
<style scoped lang='scss'>
.el-card {
  // background-color: aqua;
  border-radius: 30px;

  :deep(.el-table .cell) {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: bold;
  }
}
</style>